﻿using Elsa;
using Elsa.Activities.UserTask.Extensions;
using Elsa.Persistence.EntityFramework.Core.Extensions;
using Elsa.Persistence.EntityFramework.Sqlite;
using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using WebApi1.Application;

namespace WebApi1.Web.Core;
public class Startup :AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {


        ConfigureElsa(services);


        services.AddConsoleFormatter();

        services.AddJwt<JwtHandler>();

        services.AddCorsAccessor();

        //services.AddHttpContextAccessor();    

        //services.AddRemoteRequest();

        //services.AddEventBus();


        

        services.AddMediatR(config =>
        {
            var assemblies = Assembly.Load("WebApi1.Core");
            config.RegisterServicesFromAssemblies(assemblies);
        });


        services.AddControllers()
                .AddInjectWithUnifyResult();


       
    }


    private void ConfigureElsa(IServiceCollection services) 
    {
        services.AddApiVersioning();

        var elsaSection = App.Configuration.GetSection("Elsa");

        var baseUri = elsaSection.GetValue<string>("Server:BaseUrl");

        // Elsa services.
        services
            .AddElsa(elsa =>
            {
                elsa
                 .UseEntityFrameworkPersistence(ef => ef.UseSqlite(), true)
                .AddConsoleActivities()
                .AddUserTaskActivities()
                .AddFileActivities()
                .AddEntityActivities()
                .AddEmailActivities(elsaSection.GetSection("Smtp").Bind)                
                .AddHttpActivities(elsaSection.GetSection("Server").Bind)
                //.AddHttpActivities(opt => opt.BasePath = "/elsa-endpoint")
                .AddQuartzTemporalActivities()
                .AddJavaScriptActivities()
                .AddWorkflow<HeartbeatWorkflow>()               
                .AddWorkflowsFrom<Startup>();
                
            });

        // Elsa API endpoints.
        services.AddElsaApiEndpoints();

        // elsa api version
        services.Configure<ApiVersioningOptions>(options =>
        {
            options.UseApiBehavior = false;
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.ApiVersionReader = new UrlSegmentApiVersionReader();



        });

        services.AddCors(cors =>
        {
            cors.AddDefaultPolicy(policy =>
                                          policy.AllowAnyHeader()
                                          .AllowAnyMethod()
                                          .AllowAnyOrigin()
                                          .WithExposedHeaders("Content-Disposition"));

        });


        services.AddRazorPages();

    }



    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        // elsa designer dashboard
        app.UseStaticFiles();

        app.UseApiVersioning();     

        app.UseHttpActivities();
            

        app.UseRouting();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
           
            endpoints.MapControllers();
         
        });
    }
}
